package com.Brave.manager.Dao;

import com.Brave.manager.Daomian.Teacher;

public class TeacherDao {
    private static Teacher[] Teachers = new Teacher[8];

    static {
        Teacher tea1 = new Teacher("001", "张三", "18", "男");
        Teacher tea2 = new Teacher("002", "李四", "20", "女");
        Teachers[0] = tea1;
        Teachers[1] = tea2;
    }

    public Teacher[] findAllTeacher() {
        return Teachers;
    }

    public boolean addTeacher(Teacher tea) {
        int index = -1;
        for (int i = 0; i < Teachers.length; i++) {
            Teacher Tea = Teachers[i];
            if (Tea == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        } else {
            Teachers[index] = tea;
            return true;
        }
    }

    public void deleteTeacher(String id) {
        int index = getIndex(id);
        Teachers[index] = null;
    }

    private int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < Teachers.length; i++) {
            Teacher tea = Teachers[i];
            if(tea != null && tea.getId().equals(id)){
                index = i;
                break;
            }
        }
        return  index;
    }

    public void updateTeacher(String id, Teacher tea) {
        int index = getIndex(id);
        Teachers[index] = tea;
    }
}
